package com.lwq.im.chartdemo.handler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.lwq.im.chartdemo.bean.RequestBody;
import com.lwq.im.chartdemo.constants.Constants;
import com.lwq.im.chartdemo.session.OnlineSession;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 关闭事件
 */
@Component
@Slf4j
public class CloseHandler implements ChartRequestHandler{


    @Autowired
    private OnlineSession onlineSession;
    /**
     * 关闭事件标识
     *
     * @return
     */
    @Override
    public String getOpType() {
        return Constants.OpType.OP_TYPE_CLOSE;
    }


    /**
     * 关闭事件业务处理
     * @param channel
     * @param requestBody
     */
    @Override
    public void doHandle(Channel channel, RequestBody requestBody) {

        log.info("处理关闭事件。通道={},requestBody={} ",channel.id().asShortText(), JSON.toJSONString(requestBody));
        String userId = channel.attr(Constants.ChannelAttrKye.USER_ID).get();
        if (StrUtil.isBlank(userId)){
            return;
        }
        onlineSession.remove(channel);

    }
}
